package com.cf.user.login.controller;

import com.cf.limit.core.RequestLimit;
import com.cf.mapper.BeanMapper;
import com.cf.user.core.EnumLoginChannel;
import com.cf.user.core.part.wx.WxUserInfo;
import com.cf.user.core.part.wx.WxUserService;
import com.cf.user.login.controller.vo.request.*;
import com.cf.user.login.model.UserInfo;
import com.cf.user.login.service.TokenManager;
import com.cf.user.login.service.UserInfoService;
import com.cf.vo.ResponseVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * game-server-LoginController
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/6/13 上午 09:49
 */
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private BeanMapper beanMapper;

    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private UserInfoService userInfoService;

    @ResponseBody
    @RequestLimit(key = "'commonLogin-'+T(com.cf.util.RequestUtil).getIpAddr(#httpServletRequest)")
    @ApiOperation(value = "普通登陆", notes = "普通登陆")
    @RequestMapping(value = "/commonLogin", method = RequestMethod.POST)
    public ResponseVo<UserInfo> commonLogin(@RequestBody CommonLogin commonLogin, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        UserInfo login = userInfoService.login(commonLogin.getAccount(), commonLogin.getPwd());
        tokenManager.setToken(login, httpServletRequest, httpServletResponse);
        return ResponseVo.success(login, "登陆成功！");
    }

    @ResponseBody
    @RequestLimit(key = "'wxAppLogin-'+T(com.cf.util.RequestUtil).getIpAddr(#httpServletRequest)")
    @ApiOperation(value = "微信小程序/小游戏登陆", notes = "微信小程序/小游戏登陆")
    @RequestMapping(value = {"/wxAppLogin", "/wxGameLogin"}, method = RequestMethod.POST)
    public ResponseVo<UserInfo> commonLogin(@RequestBody WxAppLogin wxAppLogin, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        UserInfo userInfo = wxUserService.xcxLogin(wxAppLogin.getCode(), wxAppLogin.getEncrypted(), wxAppLogin.getIvo());
        tokenManager.setToken(userInfo, httpServletRequest, httpServletResponse);
        return ResponseVo.success(userInfo, "登陆成功！");
    }

    @ResponseBody
    @RequestLimit(key = "'phoneLogin-'+T(com.cf.util.RequestUtil).getIpAddr(#httpServletRequest)")
    @ApiOperation(value = "手机验证码登陆", notes = "手机验证码登陆")
    @RequestMapping(value = "/phoneLogin", method = RequestMethod.POST)
    public ResponseVo<UserInfo> phoneLogin(@RequestBody PhoneLogin phoneLogin, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        UserInfo userInfo = userInfoService.loginByPhone(phoneLogin.getPhone(), phoneLogin.getCode());
        tokenManager.setToken(userInfo, httpServletRequest, httpServletResponse);
        return ResponseVo.success(userInfo, "登陆成功！");
    }

    @ResponseBody
    @RequestLimit(key = "'wxPhoneLogin-'+T(com.cf.util.RequestUtil).getIpAddr(#httpServletRequest)")
    @ApiOperation(value = "微信app登陆", notes = "微信app登陆")
    @RequestMapping(value = "/wxPhoneLogin", method = RequestMethod.POST)
    public ResponseVo<UserInfo> wxPhoneLogin(@RequestBody WxPhoneLogin phoneLogin, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        WxUserInfo wxUserInfo = beanMapper.map(phoneLogin, WxUserInfo.class);
        UserInfo userInfo = userInfoService.loginByWxPhone(EnumLoginChannel.WXAPP.getChannel(), wxUserInfo);
        tokenManager.setToken(userInfo, httpServletRequest, httpServletResponse);
        return ResponseVo.success(userInfo, "登陆成功！");
    }

    @ResponseBody
    @RequestLimit(key = "'qqPhoneLogin-'+T(com.cf.util.RequestUtil).getIpAddr(#httpServletRequest)")
    @ApiOperation(value = "QQapp登陆", notes = "QQapp登陆")
    @RequestMapping(value = "/qqPhoneLogin", method = RequestMethod.POST)
    public ResponseVo<UserInfo> qqPhoneLogin(@RequestBody QQPhoneLogin phoneLogin, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        WxUserInfo wxUserInfo = beanMapper.map(phoneLogin, WxUserInfo.class);
        UserInfo userInfo = userInfoService.loginByWxPhone(EnumLoginChannel.QQ.getChannel(), wxUserInfo);
        tokenManager.setToken(userInfo, httpServletRequest, httpServletResponse);
        return ResponseVo.success(userInfo, "登陆成功！");
    }


}
